package com.hongding.sealcenter.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.hongding.sealcenter.response.ResponseUtil;
import com.hongding.sealcenter.service.TestService;
import com.hongding.sealcenter.vo.ApplyPfxVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("test/")
@Slf4j
public class TestController {
    @Autowired
    public TestService testService;

    @ApiOperation(value = "登录接口")
    @PostMapping("login")
    public ResponseEntity doLogin(@ApiParam("用户名") @RequestParam String username, @ApiParam("密码") @RequestParam String password) {
        if ("momoko".equals(username) && "123456".equals(password)) {
            //默认token30分钟失效
            StpUtil.login("9527", 60 * 30);
            StpUtil.checkLogin();
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            log.info("获取当前token信息：{}", tokenInfo.toString());

            return ResponseUtil.httpOk("登录成功",tokenInfo);
        }
        return ResponseUtil.httpError("登录失败");
    }

    @SaCheckLogin
    @SaCheckPermission("select")//需要操作权限
    @SaCheckRole(value = {"user", "admin"}, mode = SaMode.OR)//需要角色权限,符合其中一个角色即可通过校验
    @ApiOperation(value = "查询业务数据接口")
    @PostMapping("info")
    public ResponseEntity selectInfo() {
        return ResponseUtil.httpOk("查询成功", "123");
    }

    @ApiOperation(value = "登出接口")
    @PostMapping("logout")
    public ResponseEntity logout() {
        StpUtil.logout();
        return ResponseUtil.httpOk("退出成功");
    }

    @ApiOperation(value = "申请pfx证书接口")
    @PostMapping("applyPfx")
    public void applyPfx(ApplyPfxVo vo, HttpServletResponse response ) {
        testService.applyPfxCert(vo, response);
    }
}
